package com.itheima.zuoye.zuoye1;

import com.itheima.Xml.User;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Test {

    static Logger log =  (Logger) LoggerFactory.getLogger(com.itheima.Logback.Test.class);;

    public static void main(String[] args) throws DocumentException {
        try {
            HashMap<Integer, Users> map = new HashMap<>();
            log.info("开始解析");
            //创建解析器对象
            SAXReader sax = new SAXReader();

            //创建读取对象
            Document read = sax.read("D:\\java-base-ws\\JavaAdvanceProject\\day15_Properties\\src\\com\\itheima\\zuoye\\zuoye1\\uesers.xml");

            Element roots = read.getRootElement();

            List<Element> users = roots.elements();
            for (Element user : users) {
                String name = user.elementText("name");
                String sex = user.elementText("sex");
                String address = user.elementText("address");
                String password = user.elementText("password");
                int id = Integer.parseInt(user.attributeValue("id"));
                Users users1 = new Users();
                users1.setName(name);
                users1.setSex(sex);
                users1.setAddress(address);
                users1.setPassword(password);
                map.put(id, users1);
            }
            System.out.println("解析完毕");
            map.forEach((k, v) -> {
                System.out.println(k + "--" + v);
            });
            log.info("解析完毕");
        } catch (Exception e) {
            log.error("解析失败");
           throw new RuntimeException(e);


        }
    }
}
